﻿@{
    ViewBag.Title = "组菜单";
}
@section css{
    <style type="text/css">
        .function-item
        {
            float: left;
            display: block;
            margin-left: 5px;
            margin-right: 5px;
        }

            .function-item input
            {
                vertical-align: bottom;
            }

        td div.mini-grid-cell-inner
        {
            float: left;
        }

        .mini-grid-cell .mini-tree-treecell
        {
            vertical-align: top;
        }
    </style>
}

<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a id="one" class="mini-button" iconcls="icon-save" plain="true" onclick="saveData()">保存</a>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="mini-fit" style="height: 100%;">
    <div id="treegrid1" class="mini-treegrid" style="width: 100%; height: 100%;"
        treecolumn="MName" idfield="MID" parentfield="FID" resultastree="false"
        expandonload="true" showtreeicon="true" showcheckbox="true"
        allowselect="true" allowcellselect="false" enablehottrack="false"
        ondrawcell="ondrawcell" allowcellwrap="true">
        <div property="columns">
            <div type="indexcolumn"></div>
            <div field="IsMID" visible="false">是否具有权限</div>
            <div field="MID" visible="false"></div>
            <div field="FID" visible="false"></div>
            <div name="MName" field="MName" width="300px">模块名称</div>
            <div field="data" width="100%">权限</div>
        </div>
    </div>
</div>
<script type="text/javascript">

    mini.parse();

    var gid, showAllSelect = true;

    var tree = mini.get("treegrid1");

    function SetData(e) {
        var f = mini.clone(e);
        gid = f.gid;
        tree.setUrl("GetGroupMenu?mid=" + gid);//设置数据源
        SetCheckValue()//获取数据源
    }

    function SetCheckValue() {
        var _all = tree.getList();
        var _check = new Array();
        for (var i = 0; i < _all.length; i++) {
            if (_all[i].IsMID == "00000000-0000-0000-0000-000000000000") continue;
            _check.push(_all[i].IsMID);
        }
        tree.setValue(_check);
    }

    function ondrawcell(e) {
        var tree = e.sender,
            record = e.record,
            column = e.column,
            field = e.field,
            id = record[tree.getIdField()],
            funs = record.data;

        function createCheckboxs(funs) {
            if (!funs) return "";
            var html = "";
            if (showAllSelect) {
                var value = record.checkAll !== false ? "全选" : "取消";

                var clickFn = 'checkAllFunc(\'' + id + '\', this)';
                html += '<table><tr><td style="vertical-align:top;"><input onclick="' + clickFn + '" type="button" value="' + value + ' " class="function-item" style="border:solid 1px #aaa;" /></td><td>';
            }
            for (var i = 0, l = funs.length; i < l; i++) {
                var fn = funs[i];
                var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.ID + '\', this.checked)';
                var checked = fn.IsChecked ? 'checked' : '';
                html += '<label class="function-item"><input onclick="' + clickFn + '" ' + checked + ' type="checkbox" name="'
                        + fn.ID + '" hideFocus />' + fn.Text + '</label>';
            }
            html += '</td></tr></table>';
            return html;
        }

        if (field == 'data') {
            e.cellHtml = createCheckboxs(funs);
        }

    }

    function checkFunc(id, action, checked) {
        var record = tree.getRecord(id);
        if (!record) return;
        var funs = record.data;
        if (!funs) return;
        function getAction(action) {
            for (var i = 0, l = funs.length; i < l; i++) {
                var o = funs[i];
                if (o.ID == action) return o;
            }
        }
        var obj = getAction(action);
        if (!obj) return;
        obj.IsChecked = checked;
    }

    function checkAllFunc(id, btn) {
        var record = tree.getRecord(id);
        if (!record) return;
        var funs = record.data;
        if (!funs) return;


        var checked = record.checkAll !== false;

        for (var i = 0, l = funs.length; i < l; i++) {
            var o = funs[i];
            o.IsChecked = checked;
        }

        record.checkAll = !checked;
        tree.updateRow(record);

    }

    function saveData() {
        var modified = tree.getCheckedNodes();//选中值
        var jsonEdit = mini.encode(modified)//把数组转换为JSON
        jsonEdit = escape(jsonEdit);//编码
        tree.loading("保存中，请稍后......");
        $.ajax({
            url: "SaveGroupMenu",
            data: { GID: gid, edit: jsonEdit },
            type: "post",
            success: function (text, flag, res) {
                tree.unmask();
                if (res.responseText == "") {
                    top.mini.showTips({ content: "保存成功", state: "info", x: "center", y: "center", timeout: 1000 });
                }
                else
                    top.mini.alert(res.responseText);
            },
            error: function (text, flag, res) {
                top.mini.alert(res.responseText);
            }
        });
    }

</script>
